﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fulcrum.ViewModelGenerator
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics;

    class ViewModelPassThroughPropertyXmlConfiguration : IViewModelPassThroughPropertyConfiguration
    {
        public string Name { get; private set; }
        public string Type { get; private set; }

        public bool Settable { get; private set; }

        public string SourceObject { get; private set; }
        public string SourcePropertyName { get; private set; }

        public string Summary { get; private set; }
        public string Remarks { get; private set; }

        public ViewModelPassThroughPropertyXmlConfiguration(XElement xml)
        {
            if (null == xml)
            {
                throw new ArgumentNullException("xml");
            }

            if ("PassThroughProperty" != xml.Name)
            {
                throw new ArgumentException("Element 'PassThroughProperty' expected", "xml");
            }

            this.Parse(xml);
        }

        private void Parse(XElement xml)
        {
            Debug.Assert(null != xml && "PassThroughProperty" == xml.Name);

            this.Name = this.ParseName(xml);
            this.Type = (string)xml.RequiredAttribute("Type");

            this.Settable = (bool)xml.OptionalAttribute("Settable", false);

            this.SourceObject = (string)xml.RequiredAttribute("SourceObject");
            this.SourcePropertyName = (string) xml.OptionalAttribute("SourceProperty", this.Name);

            this.Summary = (string)xml.RequiredAttribute("Summary");
            this.Remarks = (string)xml.OptionalAttribute("Remarks", String.Empty);
        }

        private string ParseName(XElement xml)
        {
            var name = (string)xml.RequiredAttribute("Name");

            Debug.Assert(name.Length > 0);
            if (Char.IsLower(name[0]))
            {
                throw new ArgumentException("The property name must start with a uppercase letter", "Name");
            }

            return name;
        }
    }
}
